import { nameProviderModal, RouteModal, DefaultConfigModal } from '../modal';

const pageNameProvider = (
  nameProvider: nameProviderModal,
  route: RouteModal,
  fatherNode?: RouteModal,
): string => {
  if (typeof nameProvider === 'function') {
    return nameProvider(route, fatherNode);
  }
  const { proxy, after } = nameProvider;
  const fatherName = fatherNode?.name;
  const nameArray = [proxy, fatherName, route.name, after];
  return nameArray
    .filter(item => {
      return !!item;
    })
    .join('.');
}; //当前name 属性

export const localNameProvider = (
  config: DefaultConfigModal,
  route: RouteModal,
  fatherNode?: RouteModal,
) => {
  const { locals = {}, nameProvider = {} } = config;
  const { data = {} } = locals;
  const name = pageNameProvider(nameProvider, route, fatherNode);
  const keys = Object.keys(data);
  const newLocals = {};
  keys.forEach(item => {
    const itemLocalData = data[item];
    newLocals[item] = itemLocalData[name];
  });
  return {
    name,
    local: newLocals,
  };
}; //构建国际化name
